/**
 * Created by Administrator on 2015/2/6.
 */
"use strict";
angular.module("app.authenticationservice",[])
    .factory("AuthenticationService",['$http','$location',function($http,$location){
         //登录地址
         var authUrl = "/ooffice/login/oauth";
         //登录界面
         var loginUrl = "";
         //登出地址
         var logoutUrl = "";
         //登录数据
         var loginData = {};
         var service = {};
         //登录提交地址
         service.setAuthUrl = function(url){
             alert(authUrl);
             authUrl = url;
         };
         //设置登录数据
         service.setLoginData = function(data){
             loginData = data;
         };
         //登录提交地址
         service.setAuthUrl = function(url){
            authUrl = url;
         };
        service.msg;
     //登录
     service.login = function(loginData,callback){
         console.log(loginData);
         $http.post(authUrl,loginData,callback)
         .success(function(data,status,headers,config){
                 //alert(data.ret_data+"     "+data.ret_msg);
                 if(!data.ret_data) return data.ret_msg;
                 else $location.path('home');
                //callback(true,data);
         }).error(function(data,status,headers,config){
                 return '服 务 器 繁 忙 稍 后 再 试';
                //alert(data);
                //callback(false,data);
         });
    };
    //登录界面地址
    service.goLoginUI = function () {
        $location.path(loginUrl);
    };
    //登出
    service.goLogoutUI = function(){
        //$http.post(logoutUrl);
    };
    return service;
    }])
    .factory("HttpResponseInterceptor",["$rootScope","$q","$location",
        function ($rootScope,$q,$location,AuthenticationService){
             return{
                 response:function(response){
                     switch (response.status) {
                         case (200):
                             break;
                         case (500):
                             //alert("服务器系统内部错误");
                             break;
                         case (401):
                             //AuthenticationService.toLoginUI();
                             break;
                         case (403):
                             //alert("无权限执行此操作");
                             break;
                         case (408):
                             //alert("请求超时");
                             break;
                         default:
                             alert("未知错误");
                     }
                     return response;
                 }
             }
    }]);